//
//  MyAudioRecorder.m
//  录音
//
//  Created by user on 16/6/13.
//  Copyright © 2016年 jingyi. All rights reserved.
//

#import "MyAudioRecorder.h"
#import "MyHelper.h"
@implementation MyAudioRecorder

- (void)beginRecordByFileName:(NSString *)fileName {
    
    self.avSession = [MyHelper initAVAudioSession];
    //初始化recorder
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(@"%@", doc);
    //    _recordFileName = fileName;
    NSString *recordFilePath = [MyHelper getPathByFileName:fileName ofType:@"wav"];
    
    NSDictionary *recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
                                   [NSNumber numberWithFloat: 8000.0],AVSampleRateKey, //采样率
                                   [NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
                                   [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,//采样位数 默认 16
                                   [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,//通道的数目
                                   nil];
    
    NSError *err;
    _avRecorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:recordFilePath]
                                              settings:recordSetting
                                                 error:&err];
    
    //启动录音
    if (_avRecorder) {
        _avRecorder.delegate = self;
        _avRecorder.meteringEnabled = YES;
        [_avRecorder prepareToRecord];
        [_avRecorder peakPowerForChannel:0];
        [_avRecorder record];
    } else {
        NSLog(@"error: %@", err);
    }
}




#pragma mark - delegate -
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
    
    NSLog(@"完成");
    [_avSession setActive: NO error: nil];
}



@end
